home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / allison / tbitstr.cpp < prev    next >
C/C++ Source or Header  |  1993-10-10  |  2KB  |  75 lines

  1. LISTING 6 - Illustrates the Bitstring Class
  2. // tbitstr.cpp - Test the bitstring class
  3. #include <iostream.h>
  4. #include "bitstr.h"
  5. #include "string.hpp"
  6.  
  7. main()
  8. {
  9.     bitstring x(21537,4), y(string("10110"));
  10.  
  11.     cout << "Initial x: " << x << endl;
  12.     cout << "Initial y: " << y << endl;
  13.  
  14.     for (int i = 0; i <= 5; ++i)
  15.         x.set(i);
  16.     cout << "x: " << x << " (" << x.count()
  17.          << " bits set)" << endl;
  18.     cout << "x == 21567? "
  19.          << (x == bitstring(21567,x.length())) << endl;
  20.     cout << "x >>= 6 = " << (x >>= 6) << endl;
  21.     cout << "x <<= 6 = " << (x <<= 6) << endl;
  22.     cout << "x ^ 3 = " << (x ^ bitstring(3,2)) << endl;
  23.     cout << "x | 3 = " << (x | bitstring(3,2)) << endl;
  24.     cout << "x & 3 = " << (x & bitstring(3,2)) << endl;
  25.     cout << "3 & x = " << (bitstring(3,2) & x) << endl;
  26.     cout << "~x = " << (~x) << endl;
  27.  
  28.     cout << "y &= x = " << (y &= x) << endl;
  29.     cout << "y ^= x = " << (y ^= x) << endl;
  30.     cout << "y |= x = " << (y |= x) << endl;
  31.  
  32.     y.length(20);
  33.     y.reset();
  34.     for (i = 4; i <= 12; ++i)
  35.         y.set(i);
  36.     cout << "y: " << y << " (" << y.count()
  37.          << " bits set)" << endl;
  38.     cout << "x & y = " << (x & y) << endl;
  39.     cout << "x | y = " << (x | y) << endl;
  40.     cout << "x ^ y = " << (x ^ y) << endl;
  41.     cout << "x != y? " << (x != y) << endl;
  42.     cout << "x + y = " << x + y << endl;
  43.     x += y;
  44.     cout << "after x += y: " << x << endl;
  45.     x.trim();
  46.     cout << "after x.trim(): " << x << endl;
  47.  
  48.     return 0;
  49. }
  50.  
  51. /* OUTPUT:
  52. Initial x: 100001000010101
  53. Initial y: 10110
  54. x: 111111000010101 (9 bits set)
  55. x == 21567? 1
  56. x >>= 6 = 000000111111000
  57. x <<= 6 = 111111000000000
  58. x ^ 3 = 001111000000000
  59. x | 3 = 111111000000000
  60. x & 3 = 110000000000000
  61. 3 & x = 110000000000000
  62. ~x = 000000111111111
  63. y &= x = 101100000000000
  64. y ^= x = 010011000000000
  65. y |= x = 111111000000000
  66. y: 00001111111110000000 (9 bits set)
  67. x & y = 00001100000000000000
  68. x | y = 11111111111110000000
  69. x ^ y = 11110011111110000000
  70. x != y? 1
  71. x + y = 11111100000000000001111111110000000
  72. after x += y: 11111100000000000001111111110000000
  73. after x.trim(): 1111110000000000000111111111
  74. */
  75.